package com.dlwx.firefighting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.AddressListAdapter;
import com.dlwx.firefighting.bean.AddressBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 地址列表界面（仿天猫）
 */
public class AddressListActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.add_newaddress)
    TextView btn_newaddress;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.ll_empty)
    AutoLinearLayout llEmpty;

    private LRecyclerView recyclerView;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private AddressListAdapter mAdapter;
    private String userId;
    private int page = 1;
    private int requestNum = 10;
    private int count = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        initView();
        initRecy();


    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void getDatas() {
        OkGo.<String>post(Constant.ADDRESS)
                .params("user_id", userId)
                .params("num", requestNum)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<AddressBean> beans = dealResult.deal(AddressListActivity.this, response, new TypeToken<BaseBean<AddressBean,AddressBean>>() {
//                        }.getType());
//                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(AddressListActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<AddressBean> beans) {
       /* if(beans.isEmpty()){
            return;
        }*/
        mAdapter.addAll(beans);
        recyclerView.refreshComplete(requestNum);
    }

    private void initRecy() {
        recyclerView = (LRecyclerView) findViewById(R.id.addresslist_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AddressListAdapter(AddressListActivity.this);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height_6)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(true);

        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (count < (page * requestNum)) {
                    ToastUtils.toastForShort(AddressListActivity.this, getString(R.string.no_more));
                    recyclerView.setNoMore(true);
                } else {
                    page++;
                    getDatas();
                }
            }
        });
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();

            }
        });

        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                AddressBean bean = mAdapter.getDataList().get(position);
                Intent i = new Intent(AddressListActivity.this, AddAddressActivity.class);
                i.putExtra("bean", bean);
                startActivity(i);
            }
        });
    }

    /**
     * 刷新
     */
    private void refresh() {
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        page = 1;
        getDatas();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("收货地址");
       /* titleRightOneTv.setVisibility(View.VISIBLE);
        titleRightOneTv.setText("管理");*/
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.add_newaddress})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.title_right_one_tv:  //管理收货地址界面
//                startActivity(new Intent(AddressListActivity.this, AddressManagerActivity.class));
                break;
            case R.id.add_newaddress:
                Intent i = new Intent(AddressListActivity.this, AddAddressActivity.class);
                AddressBean bean = null;
                i.putExtra("bean", bean);
                startActivity(i);
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    //-----------------------------------------------------
}
